home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 266_01 / wordlib.c < prev    next >
C/C++ Source or Header  |  1990-04-19  |  5KB  |  179 lines

  1. /*    Keyword-value Text Handling Routines       File: WORDLIB.C
  2.         Robert L. Patton, Jr.
  3.         1713 Parkcrest Terrace
  4.         Arlington, TX 76012
  5.            21 April 1990
  6. */
  7. #define EQUAL(S,T) (!strcmp(S,T))
  8. #define YES 1
  9. #define NO  0
  10.  
  11. void Blank (Word, Size)
  12. /*   =====
  13.             Blank out a word */
  14. char *Word;
  15. int  Size;
  16. {
  17.   int I;
  18.   Word[0]='\0';
  19.   for (I=0; I<Size; I++)
  20.     strcat (Word," ");
  21. }
  22.  
  23. void Capitalize (Word)
  24. /*   ==========       Make word all upper case */
  25. char *Word;
  26. {
  27.   char *s;
  28.   for (s=Word; *s; s++) *s = toupper(*s);
  29. }
  30.  
  31. void Clean (Sentence)
  32. /*   =====           replace commas and equals with blanks */
  33. char *Sentence;
  34. {
  35.   while (*Sentence++ !='\0')
  36.     if (*Sentence==',' || *Sentence=='=') *Sentence=' ';
  37. }
  38.  
  39. void Ftoa (Value, Digits)
  40. /*   ====
  41.           Converts a double to a string in a very limited way.
  42.          (Developed for axis notations.)  It expects the fractional
  43.           part to be not over 3 digits and it removes any trailing
  44.           zeros and a trailing decimal point.  The user must supply
  45.           a Digits string long enough to hold the value written to
  46.           3 decimal places.
  47. */
  48. double Value;
  49. char *Digits;
  50. {
  51.   int N;
  52.   sprintf (Digits,"%.3f",Value);
  53.   N = strlen (Digits);
  54.   while (Digits[--N] == '0') {
  55.     Digits[N] = '\0';
  56.   }
  57.   if (Digits[N] == '.') Digits[N] = '\0';
  58. }
  59.  
  60. int Filler (Word)
  61. /*  ======       detect a nonsignificant word */
  62. char *Word;
  63. {
  64.   if (EQUAL (Word,"by") ||
  65.       EQUAL (Word,"is") ||
  66.       EQUAL (Word,"of") ||
  67.       EQUAL (Word,"are")  )
  68.     return YES;
  69.   else
  70.     return NO;
  71. }
  72. /*@@*/
  73. void GetInt (Sentence, Number)
  74. /*   ======    Retrieve the next word of a phrase as an integer */
  75. char *Sentence;
  76. int  *Number;
  77. {
  78.   #define IMAX 6
  79.   char Word[IMAX+1];
  80.   int  N, N1;
  81.  
  82.   *Number = 0;
  83.   GetWord (Sentence,Word,IMAX);
  84.   N1 = 0;
  85.   if (Word[0]=='+' || Word[0]=='-') N1 = 1;
  86.   for (N=N1; N<strlen(Word); N++)
  87.     if (! isdigit(Word[N])) return;
  88.   *Number = atoi(Word);
  89. }
  90.  
  91. void GetVal (Sentence, Value)
  92. /*   ======    Retrieve the next word of a phrase as a double */
  93. char *Sentence;
  94. double *Value;
  95. {
  96.   #define VMAX 15
  97.   char Word[VMAX+1], *W;
  98.   int  D, E, N, P, S;
  99.   double atof();
  100.  
  101.   *Value = 0;
  102.   strcpy (Word,"              ");
  103.   GetWord (Sentence,Word,VMAX);
  104.   D = 0; E = 0; P = 0, S = 0;
  105.   for (W=Word; *W; W++)
  106.     if (*W == '.') P++;
  107.     else if (isdigit(*W)) D++;
  108.     else if (*W == 'e' ||
  109.              *W == 'E'   ) E++;
  110.     else if (*W == '-' ||
  111.              *W == '+'   ) S++;
  112.   if (D>0 && E<=1 && P<=1 && S<=2) *Value = atof(Word);
  113. }
  114.  
  115. void GetWord (Sentence, Word, Size)
  116. /*   =======     Strips a word from the front of a string */
  117. char *Sentence,*Word;
  118. int  Size;
  119. {
  120.   int I,N;
  121.   Blank (Word,Size);
  122.   for (N=0;N<strlen(Sentence);N++)
  123.     if (Sentence[N]!=' ')
  124.     { for (I=0; I<Size; I++)
  125.       { Word[I]=Sentence[N+I];
  126.         if (Word[I]==' '||Word[I]=='\n') Word[I]='\0';
  127.         if (Word[I]=='\0') break;
  128.         Sentence[N+I]=' ';
  129.       }
  130.       if (Filler (Word)) GetWord (Sentence, Word, Size);
  131.       return;
  132.     }
  133. }
  134. int iKeyFind (psList, iListLen, sWord)
  135. /*  =========
  136.       Finds if a given word matches any entry or unique abbreviation
  137.       (consisting of the first n letters) thereof in a given word list.
  138.       The word list may be in any order for user convenience.
  139.  
  140.       returns: the position (1 to n) of the word in the list
  141.               (not to be confused with the subscript (0 to n-1))
  142.               or  0 if not found
  143.               or -1 if abbreviation is not unique
  144.       ps. This was an experiment with the Hungarian naming convention.
  145. */
  146. char **psList; /* the word list, array of strings */
  147. int  iListLen; /* the number of items in the list */
  148. char *sWord;   /* the word to try and match */
  149. {
  150. int iMatch, iTest, n;
  151.  
  152.     iTest = strlen(sWord);
  153.     iMatch = 0;
  154.     for (n=0; n<iListLen; n++) {                 /* search list */
  155.         if (!strncmp(psList[n],sWord,iTest)) {   /* partial match ? */
  156.             if (strlen(psList[n])==iTest) {      /* if exact,     */
  157.                 iMatch = n+1;                    /*    it's found */
  158.                 break;
  159.             }
  160.             if (!iMatch)                   /* if first time */
  161.                 iMatch = n+1;              /* mark the spot */
  162.             else {
  163.                 iMatch = (-1);             /* if not first time */
  164.                 break;                     /* it's not unique   */
  165.             }
  166.         }
  167.     }
  168.     return iMatch;
  169. }
  170.  
  171. void PauseMsg (Text)
  172. /*   ======        print message and pause */
  173. char *Text;
  174. {
  175.   printf("%s",Text);
  176.   printf("\n  ...\n");
  177.   getch();
  178. }
  179.